package cn.bopomo.dealer.base.action.ajax;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.bopomo.dealer.base.action.BaseAction;
import cn.bopomo.dealer.base.common.ServiceConfig;
import cn.bopomo.dealer.base.service.IOperatorService;
import cn.bopomo.dealer.base.util.SecurityUtil;
import cn.bopomo.dealer.base.vo.OperatorDetail;
import cn.bopomo.dealer.base.vo.OperatorForm;

public class AddOperatorAjax extends BaseAction {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control","no-cache");
		StringBuffer resultBuffer = new StringBuffer();
		IOperatorService os = (IOperatorService)this.findService(ServiceConfig.OPERATOR_SERVICE);
		String nUid = this.getUserId(request);
		OperatorDetail user = os.getOperatorDetail(nUid);
		
		if (user.getHasChild().equalsIgnoreCase("0")){
			resultBuffer.append("01").append("||").append("你不被允许建立子帐号");
			response.getWriter().write(resultBuffer.toString());
			return null;
		}
		OperatorForm operatorForm = (OperatorForm)form;
		
		OperatorDetail operator = new OperatorDetail();
		
		BeanUtils.copyProperties(operator,operatorForm);
		//不允许不选择操作员的所属权限组
		if (operator.getAuthorize() == null || "".equals(operator.getAuthorize())){
			resultBuffer.append("01").append("||").append("您还没有为该操作员选择权限组");
			response.getWriter().write(resultBuffer.toString());
			return null;
		}
		operator.setHasChild(operatorForm.getHasChild()? "1":"0");
		operator.setModuleAuthorize("");
		//operator.setIntroduction("");
		operator.setPassword(SecurityUtil.MD5(operator.getPassword()));
		operator.setIsDelete("0");
		String result = os.addOperator(operator);
		
		resultBuffer.append(result).append("||").append(this.getCodeDescription(result,request.getSession().getServletContext().getResource(this.codeDescXMLPath).getPath()));
		response.getWriter().write(resultBuffer.toString());
		return null;
	
		
		
		
	}
}
